package com.ews.onlineexchange.controller;

import java.io.Serializable;
import java.util.Calendar;
import java.util.Date;
import java.util.List;

import javax.ejb.Stateless;
import javax.inject.Inject;
import javax.inject.Named;
import javax.persistence.EntityManager;

import org.hibernate.CacheMode;
import org.hibernate.FlushMode;
import org.hibernate.Query;
import org.hibernate.ScrollMode;
import org.hibernate.ScrollableResults;
import org.hibernate.Session;

import com.ews.onlineexchange.model.AgentService;
import com.ews.onlineexchange.model.Bulletin;
import com.ews.onlineexchange.model.QualificationGroup;
import com.ews.onlineexchange.model.QualificationTemplate;

@Named
@Stateless
public class BulletinReqController implements Serializable {
	
	/**
	 * 
	 */
	@Inject
	EntityManager entityManager;
	@Inject
	BulletinHome3 bulletinHome3;
	private static final long serialVersionUID = 1L;

	private static final String QUERY_ALL_VALID_CERT_EQL = ""
			+ "	SELECT			b"
			+ "	FROM			Bulletin b ";
	public void updateBulletinReqStr() {
		System.out.println("===================================================================");
		System.out.println("Cert Validate Timer Runned");
		System.out.println("===================================================================");
		Date now = new Date();
		Calendar cal = Calendar.getInstance();
		
		Session session = (Session) entityManager.getDelegate();
		session.setCacheMode(CacheMode.IGNORE);
		session.setFlushMode(FlushMode.MANUAL);
		Query query = session.createQuery(QUERY_ALL_VALID_CERT_EQL);
		ScrollableResults sr = query.scroll(ScrollMode.FORWARD_ONLY);
		long count = 0L;
		
		while (sr.next()) {
			Bulletin b = (Bulletin) sr.get(0);
//			bulletinHome3.setId(b.getId());
//			Bulletin bulletin = bulletinHome3.getInstance();
			List<QualificationGroup> qualificationGroups = b.getQualificationGroup().getQualificationGroups();
			List<QualificationGroup> agentServiceGroups = b.getQualificationGroup().getAgentServiceGroups();
//			for (QualificationGroup qualificationGroup : qualificationGroups) {
//				sb.append("[");
//				List<QualificationTemplate> templates = qualificationGroup.getQualificationTemplates();
//				for (QualificationTemplate qualificationTemplate : templates) {
//						sb.append(qualificationTemplate.getDetail());
//						if(qualificationGroup.getRelation().equals(Relation.And)) {
//							sb.append("和");
//						}else {
//							sb.append("或");
//						}
//				}
//				if (sb.length() > 0)
//				sb = new StringBuilder(sb.substring(0, sb.length() - 1));
//				sb.append("]");
//				if(qualificationGroup.getRelation().equals(Relation.And)) {
//					sb.append("和");
//				}else {
//					sb.append("或");
//				}
//			}
//			
//			StringBuilder agentServiceSb = new StringBuilder();			
//			for (QualificationGroup qualificationGroup : agentServiceGroups) {
//				List<AgentService> agentServices = qualificationGroup.getAgentServices();
//				if(agentServices.size() > 0)agentServiceSb.append("[服务事项]:");
//				for (AgentService agentService : agentServices) {
//					agentServiceSb.append(agentService.getName());
//					agentServiceSb.append(";");
//				}	
//			}
//			
//			String reqStr = "";
//			if (sb.length() > 0)  reqStr = sb.substring(0, sb.length() - 1);
//			if(agentServiceSb.length() > 0) reqStr = reqStr + agentServiceSb.substring(0, agentServiceSb.length() - 1);
			
			StringBuilder sb = new StringBuilder();
			if(qualificationGroups.size() > 0) {
				for (QualificationGroup qualificationGroup : qualificationGroups) {
					for (QualificationTemplate template : qualificationGroup.getQualificationTemplates()) {
						sb.append(template.getQualificationClass().getName());
						sb.append("、");
					}
				}
			}
			if(agentServiceGroups.size() > 0) {
				for (QualificationGroup qualificationGroup : agentServiceGroups) {
					List<AgentService> services = qualificationGroup.getAgentServices();
					for (AgentService agentService : services) {
						sb.append(agentService.getName());
						sb.append("、");
					}
				}
				sb = new StringBuilder(sb.substring(0, sb.length()-1));
			}else {
				if(sb.length() > 0)sb = new StringBuilder(sb.substring(0, sb.length()-1));
			}
			
			b.setBulletinReqStr(sb.toString());
			session.update(b);
			count++;
			if (count % 200 == 0) {
				session.flush();
			}
		}
		session.flush();
	}
}
